const db = require("../config/config");
module.exports = {
  //根据用户id，，获取该用户购物车信息
  searchMemberInfo(req, res) {
   let user_name=req.query.keywords
   let user_type=req.query.role
   let query = ''
   let params = []
   if(user_type=='administrator'){
     user_type=1
   }
   else if(user_type=='member'){
    user_type=2
   }
   else if(user_type=='user'){
    user_type=3
   }
   console.log( user_name);
   console.log( user_type);
   


    

  
    if (user_name == 'undefined' && user_type == 'undefined') {
      // 查询所有记录
      query = 'SELECT * FROM user'
      params = []
      // db.dbMysql('SELECT * FROM user', [], function (err, data) {
      //   console.log(data,'data1');
      // })
    } 
    else if(user_name == 'undefined'&&user_type != 'undefined'){
      query = 'SELECT * FROM user where user_type =  ?'
      params=[user_type]
      // db.dbMysql('SELECT * FROM user where user_type like ?', [`%${user_type}%`], function (err, data) {
      //   console.log(data,'data2');
      // })

    }
     else if(user_name != 'undefined'&&user_type == 'undefined') {
        query = 'SELECT * FROM user where user_name like  ?'
        params=[`%${user_name}%`]

        // db.dbMysql('SELECT * FROM user where user_name like  ?', [`%${user_name}%`], function (err, data) {
        //   console.log(data,'data3');
        // })
      }
  
     else if (user_name != 'undefined'&&user_type != 'undefined') {
      
        query ='SELECT * FROM user where user_name like  ? AND user_type =  ?'
        params=[`%${user_name}%`, user_type]
        // db.dbMysql('SELECT * FROM user where user_name like  ? AND user_type like  ?', [`%${user_name}%`,`%${user_type}%`], function (err, data) {
        //   console.log(data,'data4');
        // })
      } 
  
    // 执行查询操作并返回结果
      db.dbMysql(query, params, function (err, data) {
          console.log(data,'data');
     
     for(let i=0;i<data.length;i++){
      if(data[i].user_enable==0){
           data[i].user_enable=true
      }
      else{
        data[i].user_enable=false
      }
      
     }
     for(let i=0;i<data.length;i++){
        if(data[i].user_type==1){
             data[i].user_type='管理员'
        }
        else if(data[i].user_type==2){
          data[i].user_type='成员'
        }
        else if(data[i].user_type==3){
            data[i].user_type='用户'

        }
    }
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: "角色管理查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "服务器错误",
        });
      }
    });
  },
}
